package com.iknow.view.widget;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.iknow.IKnow;
import com.iknow.IKnowApiV4;
import com.iknow.activity.MyWordBookActivity;
import com.iknow.data.Word;
import com.iknow.data.Word.SentInfo;
import com.iknow.db.IKnowDataBase;
import com.iknow.http.HttpException;
import com.iknow.net.translate.IKTranslateManager;
import com.iknow.task.CommonTask.WordTask;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.util.LogUtil;
import com.iknow.util.Loger;
import com.iknow.util.MsgDialog;
import com.iknow.util.StringUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class TranslatePanel extends Dialog
{
  public static Typeface FACE_PRON = null;
  private TextView desView;
  private ProgressDialog dialog;
  private Button isWordBtn;
  private TextView keyView;
  private ProgressBar loadingBar;
  private Context mContext;
  private GetWordTask mGetWordTask;
  private CommonTask.WordTask mTask;
  private TaskListener mTaskListener;
  private MediaPlayer mediaPlayer;
  private EditText memoWord;
  private MyWordBookActivity mwba;
  private ImageButton playerBtn;
  private TextView pronView;
  private ListView sentListView;
  private ListView suggListView;
  protected TranslateCallBack translateCallBack;
  private Word word = null;

  public TranslatePanel(Context paramContext)
  {
    super(paramContext, 2131427381);
    this.keyView = null;
    this.pronView = null;
    this.playerBtn = null;
    this.loadingBar = null;
    this.isWordBtn = null;
    this.desView = null;
    this.sentListView = null;
    this.suggListView = null;
    this.mediaPlayer = null;
    this.memoWord = null;
    this.mwba = null;
    this.mTaskListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "WordTask";
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        if (paramGenericTask instanceof TranslatePanel.GetWordTask)
        {
          TranslatePanel.access$5(this.this$0, TranslatePanel.access$4(this.this$0));
          if (TranslatePanel.access$2(this.this$0) != null)
            TranslatePanel.access$2(this.this$0).dismiss();
        }
        while (true)
        {
          while (true)
          {
            do
              return;
            while (!(paramGenericTask instanceof CommonTask.WordTask));
            if (paramTaskResult != TaskResult.OK)
              break;
            TranslatePanel.access$6(this.this$0);
          }
          TranslatePanel.access$7(this.this$0, ((CommonTask.WordTask)paramGenericTask).getMsg());
        }
      }

      public void onPreExecute()
      {
        if (paramGenericTask instanceof TranslatePanel.GetWordTask)
        {
          TranslatePanel.access$1(this.this$0, ProgressDialog.show(TranslatePanel.access$0(this.this$0), "", "正在进行查词，请稍候", true));
          TranslatePanel.access$2(this.this$0).setCancelable(true);
        }
        while (true)
        {
          return;
          TranslatePanel.access$3(this.this$0);
        }
      }
    };
    this.mContext = paramContext;
    setContentView(2130903072);
    Window localWindow = getWindow();
    WindowManager.LayoutParams localLayoutParams = localWindow.getAttributes();
    localLayoutParams.width = -2;
    localLayoutParams.height = -2;
    localLayoutParams.gravity = 17;
    localLayoutParams.dimAmount = 0F;
    localWindow.setAttributes(localLayoutParams);
    initView();
  }

  private boolean fillView(Word paramWord)
  {
    ArrayList localArrayList;
    Iterator localIterator;
    int i;
    if (paramWord == null)
    {
      MsgDialog.showToast(this.mContext, "没有找到翻译结果");
      i = 0;
      return i;
    }
    String str1 = paramWord.getKey();
    String str2 = paramWord.getLang();
    if (!(StringUtil.isEmpty(str2)))
    {
      this.keyView.setText(str1 + " (" + str2 + ")");
      this.pronView.setText(paramWord.getPron());
      this.desView.setText(paramWord.getDef());
      String str3 = IKnow.mIKnowDataBase.getWordMemo(str1);
      this.memoWord.setText(str3);
      localArrayList = new ArrayList();
      localIterator = paramWord.getSentList().iterator();
      if (localIterator.hasNext())
        break label273;
      String[] arrayOfString = new String[3];
      arrayOfString[0] = "orig";
      arrayOfString[1] = "trans";
      arrayOfString[2] = "audioUrl";
      int[] arrayOfInt = new int[3];
      arrayOfInt[0] = 2131296401;
      arrayOfInt[1] = 2131296402;
      arrayOfInt[2] = 2131296400;
      SimpleAdapter localSimpleAdapter = new SimpleAdapter(getContext(), localArrayList, 2130903073, arrayOfString, arrayOfInt);
      this.sentListView.setAdapter(localSimpleAdapter);
      setListener();
      if (!(paramWord.isMyWord()))
        break label349;
      this.isWordBtn.setBackgroundResource(2130837532);
      this.isWordBtn.setText("取消生词");
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
            i = 1;
          this.keyView.setText(str1);
        }
        label273: Word.SentInfo localSentInfo = (Word.SentInfo)localIterator.next();
        HashMap localHashMap = new HashMap();
        localHashMap.put("orig", localSentInfo.getOrig());
        localHashMap.put("trans", localSentInfo.getTrans());
        localHashMap.put("audioUrl", localSentInfo.getAudioUrl());
        localArrayList.add(localHashMap);
      }
      label349: this.isWordBtn.setBackgroundResource(2130837505);
      this.isWordBtn.setText("加入生词");
    }
  }

  private void onSubmiteBegin()
  {
    this.dialog = ProgressDialog.show(this.mContext, "", "正在同步云端数据，请稍候", true);
    this.dialog.setCancelable(true);
  }

  private void onSubmiteFinished()
  {
    if (this.dialog != null)
      this.dialog.dismiss();
    Toast.makeText(this.mContext, "同步完成", 0).show();
  }

  private void onSubmitefailure(String paramString)
  {
    if (this.dialog != null)
      this.dialog.dismiss();
    Toast.makeText(this.mContext, paramString, 0).show();
  }

  private void startSycn(int paramInt, List<Word> paramList)
  {
    if (IKnow.mIKnowDataBase.getUser() == null);
    while (true)
    {
      do
        return;
      while ((!(IKnow.IsNetAviable())) || ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING)));
      this.mTask = new CommonTask.WordTask(this.mContext);
      this.mTask.setListener(this.mTaskListener);
      this.mTask.setWordList(paramList);
      TaskParams localTaskParams = new TaskParams();
      localTaskParams.put("action", Integer.valueOf(paramInt));
      CommonTask.WordTask localWordTask = this.mTask;
      TaskParams[] arrayOfTaskParams = new TaskParams[1];
      arrayOfTaskParams[0] = localTaskParams;
      localWordTask.execute(arrayOfTaskParams);
    }
  }

  public void dismiss()
  {
    if (!(this.isWordBtn.getText().equals("取消生词")))
      super.dismiss();
    while (true)
    {
      Word localWord;
      String str2;
      while (true)
      {
        while (true)
        {
          return;
          localWord = IKnow.mIKnowDataBase.getWordInfo(this.word.getKey());
          String str1 = localWord.getMemo();
          str2 = this.memoWord.getText().toString();
          if (!(StringUtil.equalsString(str1, str2)))
            break;
          super.dismiss();
        }
        if (IKnow.IsNetAviable())
          break;
        Toast.makeText(this.mContext, this.mContext.getString(2131361878), 0).show();
        super.dismiss();
      }
      synchronousAgain(localWord, str2);
      super.dismiss();
    }
  }

  public void initView()
  {
    this.keyView = ((TextView)findViewById(2131296386));
    this.pronView = ((TextView)findViewById(2131296389));
    this.isWordBtn = ((Button)findViewById(2131296397));
    this.desView = ((TextView)findViewById(2131296391));
    this.sentListView = ((ListView)findViewById(2131296394));
    this.memoWord = ((EditText)findViewById(2131296396));
    this.playerBtn = ((ImageButton)findViewById(2131296387));
    this.loadingBar = ((ProgressBar)findViewById(2131296388));
    if (FACE_PRON == null)
      FACE_PRON = Typeface.createFromAsset(getContext().getAssets(), "fonts/segoeui.ttf");
    this.pronView.setTypeface(FACE_PRON);
  }

  protected void playerAudio(String paramString)
  {
    if (StringUtil.isEmpty(paramString));
    do
      return;
    while (this.mediaPlayer == null);
    this.mediaPlayer.reset();
    try
    {
      this.mediaPlayer.setDataSource(paramString);
      this.mediaPlayer.prepare();
      this.mediaPlayer.start();
    }
    catch (Exception localException)
    {
      LogUtil.e(getClass(), localException);
    }
  }

  public void setDisplay(Word paramWord)
  {
    if (isShowing());
    while (true)
    {
      do
      {
        return;
        this.mediaPlayer = new MediaPlayer();
        this.playerBtn.setVisibility(0);
        this.loadingBar.setVisibility(4);
        this.word = paramWord;
      }
      while (!(fillView(this.word)));
      show();
    }
  }

  public void setDisplay(String paramString)
  {
    if (isShowing());
    while (true)
    {
      do
      {
        return;
        this.mediaPlayer = new MediaPlayer();
        this.playerBtn.setVisibility(0);
        this.loadingBar.setVisibility(4);
        show();
      }
      while ((this.mGetWordTask != null) && (this.mGetWordTask.getStatus() == AsyncTask.Status.RUNNING));
      this.mGetWordTask = new GetWordTask(this, null);
      TaskParams localTaskParams = new TaskParams();
      localTaskParams.put("word", paramString);
      this.mGetWordTask.setListener(this.mTaskListener);
      GetWordTask localGetWordTask = this.mGetWordTask;
      TaskParams[] arrayOfTaskParams = new TaskParams[1];
      arrayOfTaskParams[0] = localTaskParams;
      localGetWordTask.execute(arrayOfTaskParams);
    }
  }

  public void setListener()
  {
    ((Button)findViewById(2131296398)).setOnClickListener(new View.OnClickListener(this)
    {
      public void onClick()
      {
        if (TranslatePanel.access$8(this.this$0) != null)
        {
          TranslatePanel.access$8(this.this$0).release();
          TranslatePanel.access$10(this.this$0, null);
        }
        this.this$0.dismiss();
      }
    });
    this.isWordBtn.setOnClickListener(new View.OnClickListener(this)
    {
      public void onClick()
      {
        if (!(IKnow.IsNetAviable()))
          Toast.makeText(TranslatePanel.access$0(this.this$0), TranslatePanel.access$0(this.this$0).getString(2131361878), 0).show();
        while (true)
        {
          ArrayList localArrayList;
          while (true)
          {
            return;
            localArrayList = new ArrayList();
            localArrayList.add(TranslatePanel.access$4(this.this$0));
            if (!(TranslatePanel.access$4(this.this$0).isMyWord()))
              break;
            IKnow.mIKnowDataBase.removeWord(localArrayList);
            TranslatePanel.access$11(this.this$0, 2, localArrayList);
            TranslatePanel.access$4(this.this$0).setIsMyWord(false);
            if (TranslatePanel.access$12(this.this$0) != null)
              TranslatePanel.access$12(this.this$0).onRefresh();
            Toast.makeText(this.this$0.getContext(), "取消生词", 0).show();
            TranslatePanel.access$13(this.this$0).setBackgroundResource(2130837505);
            TranslatePanel.access$13(this.this$0).setText("加入生词");
          }
          IKnow.mIKnowDataBase.addWord(TranslatePanel.access$4(this.this$0), TranslatePanel.access$14(this.this$0).getText().toString());
          TranslatePanel.access$11(this.this$0, 1, localArrayList);
          TranslatePanel.access$4(this.this$0).setIsMyWord(true);
          Toast.makeText(this.this$0.getContext(), "加入生词", 0).show();
          TranslatePanel.access$13(this.this$0).setBackgroundResource(2130837532);
          TranslatePanel.access$13(this.this$0).setText("取消生词");
        }
      }
    });
    this.sentListView.setOnItemClickListener(new AdapterView.OnItemClickListener(this)
    {
      public void onItemClick(, View paramView, int paramInt, long paramLong)
      {
        TextView localTextView = (TextView)paramView.findViewById(2131296400);
        ProgressBar localProgressBar = (ProgressBar)paramView.findViewById(2131296403);
        String str = StringUtil.objToString(localTextView.getText());
        if (StringUtil.isEmpty(str));
        while (true)
        {
          return;
          localProgressBar.setVisibility(0);
          new TranslatePanel.AudioFileDownThread(this.this$0, str, new TranslatePanel.SentHandler(this.this$0, localProgressBar)).start();
        }
      }
    });
    this.playerBtn.setOnClickListener(new View.OnClickListener(this)
    {
      public void onClick()
      {
        String str = TranslatePanel.access$4(this.this$0).getAudioUrl();
        if (StringUtil.isEmpty(str));
        while (true)
        {
          return;
          TranslatePanel.access$15(this.this$0).setVisibility(4);
          TranslatePanel.access$16(this.this$0).setVisibility(0);
          new TranslatePanel.AudioFileDownThread(this.this$0, str, new TranslatePanel.WordHandler(this.this$0, TranslatePanel.access$15(this.this$0), TranslatePanel.access$16(this.this$0))).start();
        }
      }
    });
  }

  public void setMyFavoriteActivity(Context paramContext)
  {
    this.mwba = ((MyWordBookActivity)paramContext);
  }

  public void synchronousAgain(Word paramWord, String paramString)
  {
    paramWord.setWordMemo(paramString);
    IKnow.mIKnowDataBase.updateWord(paramWord);
    ArrayList localArrayList = new ArrayList();
    localArrayList.add(paramWord);
    startSycn(1, localArrayList);
  }

  private class AudioFileDownThread extends HandlerThread
  {
    public static final String FILE_NAME = "AudioFileDownThread.FILE_NAME";
    private Handler handler = null;
    private String url = null;

    public AudioFileDownThread(, String paramString, Handler paramHandler)
    {
      super("AudioFileDownThread");
      this.url = paramString;
      this.handler = paramHandler;
    }

    public void run()
    {
      Object localObject;
      Looper.prepare();
      if ((this.handler == null) || (StringUtil.isEmpty(this.url)))
        return;
      try
      {
        String str = IKnow.mApi.downloadFile(this.url);
        if (str != null)
        {
          localObject = this.handler.obtainMessage(1);
          Bundle localBundle = new Bundle();
          localBundle.putString("AudioFileDownThread.FILE_NAME", str);
          ((Message)localObject).setData(localBundle);
          ((Message)localObject).sendToTarget();
        }
      }
      catch (HttpException localHttpException)
      {
        while (true)
        {
          while (true)
            Loger.e("AudioFileDownThread", localHttpException.getMessage());
          Message localMessage = this.handler.obtainMessage(0);
          localObject = localMessage;
        }
      }
    }
  }

  private class GetWordTask extends GenericTask
  {
    protected TaskResult _doInBackground()
    {
      TaskResult localTaskResult;
      TaskParams localTaskParams = paramArrayOfTaskParams[0];
      try
      {
        String str = localTaskParams.getString("word");
        TranslatePanel.access$9(this.this$0, IKnow.mTranslateManager.translate(str, false));
        if (TranslatePanel.access$4(this.this$0) != null)
        {
          TranslatePanel.access$4(this.this$0).setIsMyWord(true);
          localTaskResult = TaskResult.OK;
          return localTaskResult;
        }
      }
      catch (HttpException localHttpException)
      {
        localHttpException.printStackTrace();
        localTaskResult = TaskResult.FAILED;
      }
    }
  }

  public class SentHandler extends Handler
  {
    private ProgressBar loadBar = null;

    public SentHandler(, ProgressBar paramProgressBar)
    {
      this.loadBar = paramProgressBar;
    }

    public void handleMessage()
    {
      if (paramMessage.what == 1)
      {
        String str = paramMessage.getData().getString("AudioFileDownThread.FILE_NAME");
        this.this$0.playerAudio(str);
      }
      this.loadBar.setVisibility(4);
    }
  }

  public static abstract interface TranslateCallBack
  {
    public abstract void callBack(boolean paramBoolean);
  }

  public class WordHandler extends Handler
  {
    private ProgressBar loadBar = null;
    private ImageButton playerBtn = null;

    public WordHandler(, ImageButton paramImageButton, ProgressBar paramProgressBar)
    {
      this.loadBar = paramProgressBar;
      this.playerBtn = paramImageButton;
    }

    public void handleMessage()
    {
      if (paramMessage.what == 1)
        if (TranslatePanel.access$8(this.this$0) == null);
      while (true)
      {
        return;
        String str = paramMessage.getData().getString("AudioFileDownThread.FILE_NAME");
        this.this$0.playerAudio(str);
        this.loadBar.setVisibility(4);
        this.playerBtn.setVisibility(0);
      }
    }
  }
}